package cn.edu.hbwe.book.service.order;


import java.sql.SQLException;
import java.util.List;
import cn.edu.hbwe.book.dao.OrderDao;
import cn.edu.hbwe.book.dao.OrderItemDao;
import cn.edu.hbwe.book.dao.impl.BookDaoImpl;
import cn.edu.hbwe.book.dao.impl.OrderDaoImpl;
import cn.edu.hbwe.book.dao.impl.OrderItemDaoImpl;
import cn.edu.hbwe.book.dao.BookDao;
import cn.edu.hbwe.book.entity.Order;
import cn.edu.hbwe.book.entity.OrderItem;
import cn.edu.hbwe.book.entity.User;

public class OrderService {
	
	private OrderDao odao = new OrderDaoImpl();
	private OrderItemDao oidao = new OrderItemDaoImpl();
	private BookDaoImpl pdao = new BookDaoImpl();
	
	//根据获取到的用户实体查找订单
	public List<Order> findOrderByUser(User user) {
		List<Order> orders = null;
		try {
			orders = odao.findOrderByUser(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orders;
	}
	// 根据查询到的订单id查找订单详细信息
	public Order findOrderById(String id) {
		
		Order order = null;
		try {
			order = odao.findOrderById(id);
			List<OrderItem> items = oidao.findOrderItemByOrder(order);
			order.setOrderItems(items);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return order;
	}
	//前台删除用户订单
	public void delOrderByIdWithClient(String id) {
		try {
			
			//从订单项中查找商品购买数量
			Order order=new Order();
			order.setId(id);
			List<OrderItem> items=oidao.findOrderItemByOrder(order);
			//修改商品数量			
			pdao.updateProductNum(items);
			oidao.delOrderItems(id); //删除订单项
			odao.delOrderById(id); //删除订单
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void addOrder(Order order) {
		 OrderDaoImpl odao = new OrderDaoImpl();
		 OrderItemDaoImpl oidao = new OrderItemDaoImpl();
		 BookDaoImpl pdao = new BookDaoImpl();
		try {
			//DataSourceUtils.startTransaction();
			odao.addProduct(order);
			oidao.addOrderItem(order);
			pdao.changeProductNum(order);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
}
	
	//根据id删除订单 管理员删除订单
	public void delOrderById(String id) {			
		try {
			//从订单项中查找商品购买数量
			Order order=new Order();
			order.setId(id);
			List<OrderItem> items=oidao.findOrderItemByOrder(order);
			//修改商品数量			
			pdao.updateProductNum(items);
			oidao.delOrderItems(id); //删除订单项
			odao.delOrderById(id); //删除订单
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
	}



	// 支付成功后修改订单状态
	public void updateState(String id) {
		try {
			odao.updateOrderState(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	// 多条件查询订单信息
	public List<Order> findOrderByManyCondition(String id, String receiverName) {
		List<Order> orders = null;
		try {
			orders = odao.findOrderByManyCondition(id, receiverName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orders;
	}
	
	
	
	// 查找所有订单
	public List<Order> findAllOrder() {
		List<Order> orders = null;
		try {
			// 查找出订单信息
			orders = odao.findAllOrder();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orders;
	}

	public List<Order> findListAll(User user) throws Exception {
		OrderDaoImpl fo=new OrderDaoImpl();
		return fo.findOrderByUser(user);
	}
}
